<?php

namespace app\admin\controller;

use app\common\controller\BackBaseController;
use app\common\server\Json;
use app\common\server\Tree;

class Nav extends BackBaseController{

    protected $model_id = 'nav_id';
    protected $model_name = 'nav_name';
    protected $model_cate = 'nav_seat';
    protected $model_desc = 'nav_sort desc';
    protected $model_status = 'nav_status';


    public function add(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 判断导航类型
            if(empty($param["nav_type"])) return Json::error("导航类型不能为空");

            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if($param["nav_type"] == 1){
                if(!$validate->scene("add_jump")->check($param)) return Json::error($validate->getError());
            }else{
                if(!$validate->scene("add_not")->check($param)) return Json::error($validate->getError());
            }


            ## 获取默认的状态
            $param[$this->model_status] = config("project.mysql_code.table_pending");


            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            return view();
        }
    }


    public function edit(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 判断导航类型
            if(empty($param["nav_type"])) return Json::error("导航类型不能为空");

            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if($param["nav_type"] == 1){
                if(!$validate->scene("edit_jump")->check($param)) return Json::error($validate->getError());
            }else{
                if(!$validate->scene("edit_not")->check($param)) return Json::error($validate->getError());
            }

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            $nav_id = $this->request->param($this->model_id,0,'intval');
            if(!empty($nav_id)){
                ## 调用业务层获取单条数据
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array($this->model_id => $nav_id));
                ## 获取当前导航类型下的导航数据
                if(!empty($info)){
                    $navLists = $class::getArrayLists(array("nav_seat" => $info["nav_seat"],'nav_status' => config("project.mysql_code.table_normal")),'nav_id,nav_name,pid');
                }

                return view('',[
                    'info' => $info,
                    'navs' => Tree::getNavTree($navLists,0,'--',0)
                ]);
            }
        }
    }




    public function getNavLists(){
        if($this->request->isGet()){
            $seat_id = $this->request->param("seat_id",0,'intval');
            if(!empty($seat_id)){
                $class = self::getBusExample($this->request->controller());
                $lists = $class::getArrayLists(array("nav_seat" => $seat_id,'nav_status' => config("project.mysql_code.table_normal")),'nav_id,nav_name,pid');
                return Json::success("获取成功",Tree::getNavTree($lists,0,'--',0));
            }

        }else{
            return Json::error("请求方式错误");
        }
    }

}